﻿@using Knlght.Web.Infrastructure.Menus;


@helper createMenu(MenuItem[] menuitems, MenuItem[] locations)
{
foreach (var mi in menuitems)
{
    if (mi.Children != null && mi.Children.Count > 0)
    {
            <li>
                <a href="#"><i class="fa @mi.IconClass "></i><span class="nav-label">@mi.Title</span><span class="fa arrow"></span></a>
                @if (mi.Children != null && mi.Children.Count > 0)
                {
                    <ul class="nav nav-second-level">
                        @foreach (var child1 in mi.Children)
                        {
                            if (child1.Children != null && child1.Children.Count > 0)
                            {
                                <li>
                                    <a href="#"><i class="fa @child1.IconClass"></i><span class="nav-label">@child1.Title</span><span class="fa arrow"></span></a>
                                    <ul class="nav nav-second-level">
                                        @foreach (var child2 in child1.Children)
                                        {
                                            <li>
                                                <a class="J_menuItem" href="@child2.Url">@child2.Title</a>
                                            </li>
                                        }
                                    </ul>
                                </li>
                            }
                            else
                            {
                                <li>
                                    <a class="J_menuItem" href="@child1.Url">@child1.Title</a>
                                </li>
                            }
                        }
                    </ul>
                }
            </li>
    }
    else
    {
            <li>
                <a href="@mi.Url"><i class="fa @mi.IconClass "></i><span class="nav-label">@mi.Title</span></a>
            </li>
    }
}
}
@{
    #region csm 2016-5-24 新增过滤掉没有权限的菜单
    var service = new Knlght.Service.AdministratorsService();
    var response = service.GetModel(this.User.Identity.Name);
    if (response.IsSuccess && response.Model != null)
    {
        if (!response.Model.IsSuper)
        {
            var query = new Knlght.Domain.QueryRequest<Knlght.Model.Sys.RoleInfo>();
            query.Add(o => o.Id == response.Model.OwmRoleId);
            var rep = new Knlght.Service.RoleInfoService().GetModel(query);
            if (rep.IsSuccess && rep.Model != null)
            {
                List<MenuItem> newMenu = new List<MenuItem>();
                var arr = rep.Model.Jurisdiction.Split(',');
                foreach (var item in ViewBag.Menu as MenuItem[])
                {
                    if (item.ID == "Home")
                    {
                        newMenu.Add(item);
                        continue;
                    }
                    if (item.Children != null && item.Children.Count > 0)
                    {
                        bool bl = false;
                        var newModel = new MenuItem();
                        var newList = new List<MenuItem>();
                        foreach (var item2 in item.Children)
                        {
                            if (arr.Contains(item2.ID))
                            {
                                bl = true;
                                newModel = item.Clone();
                                newList.Add(item2);
                            }
                        }
                        if (bl)
                        {
                            newModel.Children = newList;
                            newMenu.Add(newModel);
                        }
                    }
                }
                ViewBag.Menu2 = newMenu.ToArray();
            }
            else
            {
                Response.Write("<script>alert('没权限访问'); location.href = '/Manage/Home/Index'</script>");
            }
        }
        else
        {
            ViewBag.Menu2 = ViewBag.Menu;
        }
    }
    else
    {
        //取消Session会话
        Session.Abandon();
        //删除Forms验证票证
        System.Web.Security.FormsAuthentication.SignOut();
        Response.Write("<script>alert('操作超时，请重新登录'); location.href = '/Manage/Account/Login'</script>");

    }
    #endregion
}
@if (response.IsSuccess && response.Model != null)
{
    @createMenu(ViewBag.Menu2, ViewBag.Locations)
}
